package com.jxs.android.weixi;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weixi.data.Filter_Data_Common;
import com.telecom.weixi.R;

/**
 * @Des 用户
 * @Author jiangxs
 * @Des 2012-11-21 上午9:25:54
 */
public class MyCustomer_User_Laucher_UserLosed extends Start_Older_Activity_Abstract_Loading {

	private String conditionProduct = Filter_Data_Common.getConditionProduct();// 查询条件-产品类型：全部(空),CDMA(7),固话(6),小灵通(3),宽带(2),其它(0)
	private String conditionHighValue = Filter_Data_Common.getConditionHighValue();// 查询条件-11年锁定移动中高值用户:全部(空),是(1),否(0)
	private String conditionVipClub = Filter_Data_Common.getConditionVipClub();// 查询条件-客户级别:全部(空),钻石卡(4),金卡(3),银卡(2),一般客户(1)
	private String conditionAccNbr = Filter_Data_Common.getConditionAccNbr();// 查询条件-手机号码：不限定号码时为空，限定号码需输入号码
	private String conditionSortFlag = "1";// 排序标示，1入网日期，2合账用户数，3账户ARPU，4账户余额

	private CstLsdUserAdapter adapter;
	private SortBroadCastReciver receiver = new SortBroadCastReciver();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Filter_Data_Common.setConditionAccNbr("");
		Filter_Data_Common.setConditionAddStock("");
		Filter_Data_Common.setConditionHighValue("");
		Filter_Data_Common.setConditionProduct("");
		Filter_Data_Common.setConditionVipClub("");
		Filter_Data_Common.setConditionSortTag(conditionSortFlag);
	}

	@Override
	protected void onResume() {
		super.onResume();
		registerReceiver(receiver, new IntentFilter(MyLaucher.SORT_ACTION));
	}

	@Override
	public int getLayout() {
		return R.layout.bu_common_list_no_top_bar;
	}

	@Override
	public void loadingData(int currentPage, boolean showDialog) {
		getWebServiceData(WeixiApplication.getUserInfo().getTicket(), Which.MY_CUSTOMER_USER_LOST, showDialog, new DataInitListeren() {
			@Override
			public void initData(BuLoadingDailog progressDialog, boolean isShow) {
				progressDialog.dismiss();
			}
		}, conditionProduct, conditionHighValue, conditionVipClub, conditionAccNbr, conditionSortFlag, PAGE_SIZE, currentPage);
	}

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new CstLsdUserAdapter(context);
		return adapter;
	}

	@Override
	public Object paserData(SoapObject retMsgObject, Object... params) {
		List<CstLsdUser> tems = new ArrayList<CstLsdUser>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				CstLsdUser cstInfo = new CstLsdUser();

				cstInfo.serv_id = taskBeanObject.getPropertySafelyAsString("serv_id");// 用户编号
				cstInfo.cust_id = taskBeanObject.getPropertySafelyAsString("cust_id");// 用户编号
				cstInfo.acc_nbr = taskBeanObject.getPropertySafelyAsString("acc_nbr");// 手机号码
				cstInfo.serv_trait_name = taskBeanObject.getPropertySafelyAsString("serv_trait_name");//
				cstInfo.serv_cust_type_name = taskBeanObject.getPropertySafelyAsString("serv_cust_type_name");// 用户客户类型
				cstInfo.cust_sub_brand_name = taskBeanObject.getPropertySafelyAsString("cust_sub_brand_name");// 客户品牌
				cstInfo.serv_name = taskBeanObject.getPropertySafelyAsString("serv_name");// 用户名称
				cstInfo.lost_date = taskBeanObject.getPropertySafelyAsString("lost_date");// 入网日期
				cstInfo.serv_state_name = taskBeanObject.getPropertySafelyAsString("serv_state_name");// 用户状态
				cstInfo.join_acct_serv_num = taskBeanObject.getPropertySafelyAsString("join_acct_serv_num");// 合帐用户数
				cstInfo.acctarpu = taskBeanObject.getPropertySafelyAsString("acctarpu");// 账户ARPU
				cstInfo.pre_pay_bal = taskBeanObject.getPropertySafelyAsString("pre_pay_bal");// 账户余额
				cstInfo.cur_mon_flow = taskBeanObject.getPropertySafelyAsString("cur_mon_flow");// 3G本月流量
				cstInfo.last_mon_flow = taskBeanObject.getPropertySafelyAsString("last_mon_flow");// 上月流量

				tems.add(cstInfo);
			}
		}
		return tems;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateList(Message msg) {
		List<CstLsdUser> tems = (List<CstLsdUser>) msg.obj;
		empytText.setVisibility(View.GONE);

		if (currentPage == 1) {
			if (tems.size() < 1) {
				empytText.setVisibility(View.VISIBLE);
				refreshListView.removeFooterView(loadMoreView);
			}
		}
		adapter.setCstLsdUsers(tems, currentPage);
		adapter.notifyDataSetChanged();
		if (tems.size() < PAGE_SIZE) {
			loadMoreButton.setText("数据已全部获取完毕");
			loadMoreButton.setEnabled(false);
		} else {
			loadMoreButton.setText("查看更多...");
			currentPage++;
		}

	}

	class CstLsdUserAdapter extends BaseAdapter {

		List<CstLsdUser> custInfos = new ArrayList<CstLsdUser>();

		private LayoutInflater inflater = null;
		private ViewHolder holder = null;

		public CstLsdUserAdapter(Context context) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public void setCstLsdUsers(List<CstLsdUser> custInfos, int currentPage) {
			if (currentPage == 1) {
				this.custInfos.clear();
			}
			this.custInfos.addAll(custInfos);
		}

		@Override
		public int getCount() {
			return custInfos.size();
		}

		@Override
		public CstLsdUser getItem(int position) {
			return custInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final CstLsdUser custInfo = custInfos.get(position);

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.bu_list_item_cst_lsd_user, null);
				holder = new ViewHolder();

				holder.position = (TextView) convertView.findViewById(R.id.position);// 用户编号
				holder.serv_id = (TextView) convertView.findViewById(R.id.serv_id);// 用户编号
				holder.acc_nbr = (TextView) convertView.findViewById(R.id.acc_nbr);// 手机号码
				holder.serv_trait_name = (TextView) convertView.findViewById(R.id.serv_trait_name);//
				holder.serv_cust_type_name = (TextView) convertView.findViewById(R.id.serv_cust_type_name);// 用户客户类型
				holder.cust_sub_brand_name = (TextView) convertView.findViewById(R.id.cust_sub_brand_name);// 客户品牌
				holder.serv_name = (TextView) convertView.findViewById(R.id.serv_name);// 用户名称
				holder.lost_date = (TextView) convertView.findViewById(R.id.lost_date);// 入网日期
				holder.serv_state_name = (TextView) convertView.findViewById(R.id.serv_state_name);// 用户状态
				holder.join_acct_serv_num = (TextView) convertView.findViewById(R.id.join_acct_serv_num);// 合帐用户数
				holder.acctarpu = (TextView) convertView.findViewById(R.id.acctarpu);// 账户ARPU
				holder.pre_pay_bal = (TextView) convertView.findViewById(R.id.pre_pay_bal);// 账户余额
				holder.cur_mon_flow = (TextView) convertView.findViewById(R.id.cur_mon_flow);// 3G本月流量
				holder.last_mon_flow = (TextView) convertView.findViewById(R.id.last_mon_flow);// 上月流量
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.position.setText("No." + (position + 1));// 序号

			holder.serv_id.setText(custInfo.serv_id);// 用户编号
			holder.acc_nbr.setText(custInfo.acc_nbr);// 手机号码
			holder.serv_trait_name.setText(custInfo.serv_trait_name);//
			holder.serv_cust_type_name.setText(custInfo.serv_cust_type_name);// 用户客户类型
			holder.cust_sub_brand_name.setText(custInfo.cust_sub_brand_name);// 客户品牌
			holder.serv_name.setText(custInfo.serv_name);// 用户名称
			holder.lost_date.setText(custInfo.lost_date);// 入网日期
			holder.serv_state_name.setText(custInfo.serv_state_name);// 用户状态
			holder.join_acct_serv_num.setText(custInfo.join_acct_serv_num);// 合帐用户数
			holder.acctarpu.setText(custInfo.acctarpu);// 账户ARPU
			holder.pre_pay_bal.setText(custInfo.pre_pay_bal);// 账户余额
			holder.cur_mon_flow.setText(custInfo.cur_mon_flow);// 3G本月流量
			holder.last_mon_flow.setText(custInfo.last_mon_flow);// 上月流量
			convertView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					startOldNew1Activity(custInfo.serv_id, custInfo.cust_id, custInfo.acc_nbr);

				}
			});
			return convertView;
		}

		class ViewHolder {
			TextView position;// 序号
			TextView serv_id;// 用户编号
			TextView cust_id;// 客户编号
			TextView acc_nbr;// 手机号码
			TextView serv_trait_name;//
			TextView serv_cust_type_name;// 用户客户类型
			TextView cust_sub_brand_name;// 客户品牌
			TextView serv_name;// 用户名称
			TextView lost_date;// 入网日期
			TextView serv_state_name;// 用户状态
			TextView join_acct_serv_num;// 合帐用户数
			TextView acctarpu;// 账户ARPU
			TextView pre_pay_bal;// 账户余额
			TextView cur_mon_flow;// 3G本月流量
			TextView last_mon_flow;// 上月流量
		}

	}

	class CstLsdUser {
		String serv_id;// 用户编号
		String cust_id;// 客户编号
		String acc_nbr;// 手机号码
		String serv_trait_name;//
		String serv_cust_type_name;// 用户客户类型
		String cust_sub_brand_name;// 客户品牌
		String serv_name;// 用户名称
		String lost_date;// 入网日期
		String serv_state_name;// 用户状态
		String join_acct_serv_num;// 合帐用户数
		String acctarpu;// 账户ARPU
		String pre_pay_bal;// 账户余额
		String cur_mon_flow;// 3G本月流量
		String last_mon_flow;// 上月流量
	}

	@Override
	public String getMethod() {
		return "getServLostPage";
	}

	private class SortBroadCastReciver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (null != intent && null != intent.getExtras()) {
				conditionSortFlag = Filter_Data_Common.getConditionSortTag();// 排序标示，1入网日期，2合账用户数，3账户ARPU，4账户余额
				conditionProduct = Filter_Data_Common.getConditionProduct();// 查询条件-产品类型：全部(空),CDMA(7),固话(6),小灵通(3),宽带(2),其它(0)
				conditionHighValue = Filter_Data_Common.getConditionHighValue();// 查询条件-11年锁定移动中高值用户:全部(空),是(1),否(0)
				conditionVipClub = Filter_Data_Common.getConditionVipClub();// 查询条件-客户级别:全部(空),钻石卡(4),金卡(3),银卡(2),一般客户(1)
				conditionAccNbr = Filter_Data_Common.getConditionAccNbr();// 查询条件-手机号码：不限定号码时为空，限定号码需输入号码
				currentPage = 1;
				loadMoreButton.setEnabled(true);
				loadMoreButton.setText("正在加载中..."); // 设置按钮文字
				loadingData(currentPage, true);
			}
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(receiver);
	}

}
